package ge.lowlevel.js;

 



import ge.exceptions.FunctionNonSuported;
import ge.lowlevel.DrawEngine;

import com.google.gwt.core.client.JavaScriptObject;
 

public class JSDrawEngine extends DrawEngine 
{
	
	private JavaScriptObject canvas;
	private JavaScriptObject context;
	public JSDrawEngine()
	{
		init();
	}
 
	
	private native void init()
	/*-{
		 
		 $wnd._canvas = $doc.getElementsByTagName('canvas');
 		 $wnd._loadfiles = new Array();
 		 $wnd._images = new Array();
		 $wnd._context = new Array();
		 for( i = 0; i < $wnd._canvas.length; i++)
		 {
		 	$wnd._context[i] = $wnd._canvas[i].getContext('2d');
		 }
		 	
		$wnd._layer = 0;
		
	
	 }-*/;
	@Override
	public native boolean isImageLoaded(String imageUrl)
	/*-{
	 		if(imageUrl in $wnd._loadfiles && $wnd._loadfiles[imageUrl])
				return $wnd._loadfiles[imageUrl];
			return false;
	}-*/;
	@Override 
	public native void loadImage(String imageUrl)
	/*-{
	
		$wnd._loadfiles[imageUrl] = false;
		var nuevaImagen = new Image();
		nuevaImagen.src = imageUrl;
	 	
		var loadFunc =  function()
		{
			
			if(nuevaImagen.complete)
			{
				
				$wnd._loadfiles[imageUrl] = true;
				$wnd._images[imageUrl] = nuevaImagen;
			}
			else
			{
				//$wnd.alert("Imagen "+imageUrl+" "+nuevaImagen.complete);
				var t=setTimeout(loadFunc,3000);
			}
		}
		nuevaImagen.onLoad = loadFunc();
	}-*/;
	@Override
	public native void setLayer(int layer)
	/*-{
	 	$wnd._layer = layer;
	 }-*/;
	@Override
	public native int getNumLayers()
	/*-{
 			return  $wnd._canvas.length;
 	}-*/;
	private native void clearNative()
	/*-{
	 		 for( i = 0; i < $wnd._canvas.length; i++)
	 			$wnd._context[i].clearRect(0, 0, $wnd._canvas[i].width,$wnd._canvas[i].height);
	}-*/;
	
	
	@Override
	public void clear()
	{
		clearNative();
	}
	
	
	@Override
	public native void drawFrame(String url, double canvasX, double canvasY,
			double canvasWith, double canvasHeight, double spriteX, double spriteY,
			double frameWidth, double frameHeight) throws FunctionNonSuported 
	/*-{
	 		var imagen;
	 		if(url in $wnd._images)
	 		{
	 			imagen = $wnd._images[url];
	 		}
	 		else
	 		{
		 		imagen = new Image();
				imagen.src = url;
				$wnd._images[url] = imagen;
			}
			//if(!imagen.complete)
			//	$wnd.alert(url+" no completado");
			
			
				//$wnd.alert(url);
				$wnd._context[$wnd._layer].drawImage(imagen, spriteX, spriteY, frameWidth, frameHeight, canvasX,canvasY, canvasWith, canvasHeight);	
				return;
	}-*/;	
		
	@Override
	public native void moveTo(double x, double y)
	/*-{
		$wnd._context[$wnd._layer].moveTo(x,y);
	}-*/;
	
	public native void lineTo(double x, double y)
	/*-{
		$wnd._context[$wnd._layer].lineTo(x,y);
		
	}-*/;
	
	@Override
	public native void drawImage(String url, double canvasX, double canvasY)
			 
	/*-{
	  
	 	var imagen = new Image();
		imagen.src = url;
		$wnd._context[$wnd._layer].drawImage(imagen, canvasX, canvasY);
	
		return;
		
	}-*/;	
	
	@Override
	public native void drawRectangle(double x1, double y1, double w, double h)
			throws FunctionNonSuported 
	/*-{
		$wnd._context[$wnd._layer].fillRect (x1, y1, w, h); 
		return;
	}-*/;
	@Override
	public native void setColor(int R, int G, int B, int A) 
	/*-{
		var a = 255;
		 
		if(A < 255)
		{
			a = A/255;
		}
    	$wnd._context[$wnd._layer].fillStyle = "rgba("+R+","+G+","+B+","+a+")";
    	return;
    }-*/;	  
	public native void drawText(String text, double x, double y, String font)
			throws FunctionNonSuported 
	/*-{
	    $wnd._context[$wnd._layer].font = font || 	"bold 14px sans-serif"; // Por defecto
	    $wnd._context[$wnd._layer].fillText(text, x, y);
	    return;
	}-*/;	
	@Override
	public native double getWidth()
	/*-{
    	 return $wnd._canvas[$wnd._layer].width;
 
    }-*/;	
	@Override
	public native double getHeight()
	/*-{
    	 return $wnd._canvas[$wnd._layer].height;
  
    }-*/;


	@Override
	public native void setPath(boolean start) 
	/*-{
	 		if(start)
	 			$wnd._context[$wnd._layer].beginPath();
	 		else
	 			$wnd._context[$wnd._layer].closePath();
	}-*/;


	@Override
	public native void fill() 
	/*-{
		 $wnd._context[$wnd._layer].fill();
	}-*/;


	@Override
	public native void stroke() 
	/*-{
	 	$wnd._context[$wnd._layer].stroke();
	}-*/;


	@Override
	public native void drawCircle(double x, double y, double radio)
	/*-{
 			var ctx = $wnd._context[$wnd._layer];
 			//$wnd.alert(x+" , " +y+" ," +radio);
 			ctx.beginPath();
			ctx.arc(x,y,radio,0,Math.PI*2,true);
			ctx.closePath();
	}-*/;
	
	
	
}
